﻿using System;
using System.Data;
using System.Data.SqlClient;
using Bizspoke.BizTalk.Adapter;

namespace Bizspoke.BizTalk.HATPlugIn
{
    public class MessageIdResolver
    {
        private Guid _ActivityId = Guid.Empty;
        private string _BizTalkHost = "";
        private string _ConnectionString = "";
        private string _MsgBoxDB = "";
        private string _MsgBoxServer = "";

        public DataTable ExecuteQuery(string query)
        {
            SqlConnection connection = this.GetConnection();
            SqlCommand selectCommand = connection.CreateCommand();
            selectCommand.Connection = connection;
            selectCommand.CommandType = CommandType.StoredProcedure;
            selectCommand.CommandText = "sp_executesql";
            selectCommand.Parameters.Clear();
            selectCommand.Parameters.Add("@statement", query);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            return dataTable;
        }

        public Guid FindOtherId(Guid msgInstId)
        {
            DataTable table = SqlHelper.ExecuteQuery(string.Format("SELECT uidMessageInstanceId FROM dta_MessageInOutEvents WHERE uidMessageInstanceId <> '{0}' AND uidActivityId = '{1}'", msgInstId.ToString(), this.ActivityId.ToString()), new BtsTrackingDbInfo());
            if ((table != null) && (table.Rows.Count > 0))
            {
                DataRow row = table.Rows[0];
                return (Guid) row["uidMessageInstanceId"];
            }
            return Guid.Empty;
        }

        public Guid FindWorkId(Guid msgInstId)
        {
            string str = "SELECT [uidWorkID] FROM [";
            string str2 = "] WHERE [uidMessageID] = '" + msgInstId.ToString() + "'";
            DataRow[] rowArray = this.ExecuteQuery(str + this.WorkQTable + str2).Select();
            if (rowArray.Length > 0)
            {
                return (Guid) rowArray[0].ItemArray[0];
            }
            rowArray = this.ExecuteQuery(str + this.SuspendedQTable + str2).Select();
            if (rowArray.Length > 0)
            {
                return (Guid) rowArray[0].ItemArray[0];
            }
            rowArray = this.ExecuteQuery(str + this.ScheduledQTable + str2).Select();
            if (rowArray.Length > 0)
            {
                return (Guid) rowArray[0].ItemArray[0];
            }
            return new Guid("00000000-0000-0000-0000-000000000000");
        }

        public SqlConnection GetConnection()
        {
            this._ConnectionString = "packet size=4096;integrated security=SSPI;persist security info=True;data source=" + this._MsgBoxServer + ";initial catalog=" + this._MsgBoxDB;
            return new SqlConnection(this._ConnectionString);
        }

        public Guid ActivityId
        {
            get
            {
                return this._ActivityId;
            }
            set
            {
                this._ActivityId = value;
            }
        }

        public string BizTalkHost
        {
            get
            {
                return this._BizTalkHost;
            }
            set
            {
                this._BizTalkHost = value;
            }
        }

        public bool HasActivityInfo
        {
            get
            {
                return (0 != this._ActivityId.CompareTo(Guid.Empty));
            }
        }

        public bool HasMsgBoxInfo
        {
            get
            {
                return (((this._MsgBoxServer != "") && (this._MsgBoxDB != "")) && (this._BizTalkHost != ""));
            }
        }

        public string MsgBoxDB
        {
            get
            {
                return this._MsgBoxDB;
            }
            set
            {
                this._MsgBoxDB = value;
            }
        }

        public string MsgBoxServer
        {
            get
            {
                return this._MsgBoxServer;
            }
            set
            {
                this._MsgBoxServer = value;
            }
        }

        public string ScheduledQTable
        {
            get
            {
                return (this._BizTalkHost + "Q_Scheduled");
            }
        }

        public string SuspendedQTable
        {
            get
            {
                return (this._BizTalkHost + "Q_Suspended");
            }
        }

        public string WorkQTable
        {
            get
            {
                return (this._BizTalkHost + "Q");
            }
        }
    }
}

